package com.ruoyi.production.service.impl.pesticide;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.domain.carton.CartonAmount;
import com.ruoyi.production.domain.carton.CartonPurchase;
import com.ruoyi.production.domain.pesticide.PesticideAmount;
import com.ruoyi.production.mapper.pesticide.PesticideAmountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.pesticide.PesticidePurchaseMapper;
import com.ruoyi.production.domain.pesticide.PesticidePurchase;
import com.ruoyi.production.service.pesticide.IPesticidePurchaseService;

/**
 * 农药购买信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Service
public class PesticidePurchaseServiceImpl implements IPesticidePurchaseService
{
    @Autowired
    private PesticidePurchaseMapper pesticidePurchaseMapper;
    @Autowired
    private PesticideAmountMapper pesticideAmountMapper;

    /**
     * 查询农药购买信息
     *
     * @param pesticidePurchaseId 农药购买信息ID
     * @return 农药购买信息
     */
    @Override
    public PesticidePurchase selectPesticidePurchaseById(Long pesticidePurchaseId)
    {
        return pesticidePurchaseMapper.selectPesticidePurchaseById(pesticidePurchaseId);
    }

    /**
     * 查询农药购买信息列表
     *
     * @param pesticidePurchase 农药购买信息
     * @return 农药购买信息
     */
    @Override
    public List<PesticidePurchase> selectPesticidePurchaseList(PesticidePurchase pesticidePurchase)
    {
        return pesticidePurchaseMapper.selectPesticidePurchaseList(pesticidePurchase);
    }

    /**
     * 新增农药购买信息
     *
     * @param pesticidePurchase 农药购买信息
     * @return 结果
     */
    @Override
    public int insertPesticidePurchase(PesticidePurchase pesticidePurchase)
    {
        return pesticidePurchaseMapper.insertPesticidePurchase(pesticidePurchase);
    }

    /**
     * 修改农药购买信息
     *
     * @param pesticidePurchase 农药购买信息
     * @return 结果
     */
    @Override
    public int updatePesticidePurchase(PesticidePurchase pesticidePurchase)
    {
        Long pesticidePurchaseId = pesticidePurchase.getPesticidePurchaseId();
        //数据库存储的数据
        PesticidePurchase PurchaseDB = selectPesticidePurchaseById(pesticidePurchaseId);
        //数据库存储的原数据的总量
        PesticideAmount originAmounts = new PesticideAmount();
        originAmounts.setPesticideNameId(PurchaseDB.getPesticideNameId());
        originAmounts.setPesticideTypeId(PurchaseDB.getPesticideTypeId());
        originAmounts.setOriginCompany(PurchaseDB.getOriginCompany());
        originAmounts.setContent(PurchaseDB.getContent());
        List<PesticideAmount> amounts = pesticideAmountMapper.selectPesticideAmountList(originAmounts);
        PesticideAmount originAmount = amounts.get(0);
        if (PurchaseDB.getPesticideNameId().equals(pesticidePurchase.getPesticideNameId()) &&
                PurchaseDB.getPesticideTypeId().equals(pesticidePurchase.getPesticideTypeId()) &&
                PurchaseDB.getContent().equals(pesticidePurchase.getContent()) &&
                PurchaseDB.getOriginCompany().equals(pesticidePurchase.getOriginCompany())
        ) {
            originAmount.setStorageAmount(originAmount.getStorageAmount()+pesticidePurchase.getPurchaseAmount()-PurchaseDB.getPurchaseAmount());
            pesticideAmountMapper.updatePesticideAmount(originAmount);
        }else {
            PesticideAmount newAmounts = new PesticideAmount();
            newAmounts.setPesticideNameId(pesticidePurchase.getPesticideNameId());
            newAmounts.setPesticideTypeId(pesticidePurchase.getPesticideTypeId());
            newAmounts.setOriginCompany(pesticidePurchase.getOriginCompany());
            newAmounts.setContent(pesticidePurchase.getContent());
            List<PesticideAmount> amounts1 = pesticideAmountMapper.selectPesticideAmountList(newAmounts);
            if (StringUtils.isEmpty(amounts1)){
                newAmounts.setStorageAmount(pesticidePurchase.getPurchaseAmount());
                pesticideAmountMapper.insertPesticideAmount(newAmounts);
            }else {
                PesticideAmount newAmount = amounts1.get(0);
                newAmount.setStorageAmount(newAmount.getStorageAmount()+pesticidePurchase.getPurchaseAmount());
                pesticideAmountMapper.updatePesticideAmount(newAmount);
            }
            originAmount.setStorageAmount(originAmount.getStorageAmount()-PurchaseDB.getPurchaseAmount());
            pesticideAmountMapper.updatePesticideAmount(originAmount);
        }
        return pesticidePurchaseMapper.updatePesticidePurchase(pesticidePurchase);
    }

    /**
     * 批量删除农药购买信息
     *
     * @param pesticidePurchaseIds 需要删除的农药购买信息ID
     * @return 结果
     */
    @Override
    public int deletePesticidePurchaseByIds(Long[] pesticidePurchaseIds)
    {
        for (Long pesticidePurchaseId : pesticidePurchaseIds) {
            PesticidePurchase pesticidePurchase = selectPesticidePurchaseById(pesticidePurchaseId);
            PesticideAmount pesticideAmount = new PesticideAmount();
            pesticideAmount.setPesticideTypeId(pesticidePurchase.getPesticideTypeId());
            pesticideAmount.setOriginCompany(pesticidePurchase.getOriginCompany());
            pesticideAmount.setPesticideNameId(pesticidePurchase.getPesticideNameId());
            pesticideAmount.setContent(pesticidePurchase.getContent());
            List<PesticideAmount> amounts = pesticideAmountMapper.selectPesticideAmountList(pesticideAmount);
            PesticideAmount pesticideAmount1 = amounts.get(0);
            pesticideAmount1.setStorageAmount(pesticideAmount1.getStorageAmount() - pesticidePurchase.getPurchaseAmount());
            pesticideAmountMapper.updatePesticideAmount(pesticideAmount1);}
            return pesticidePurchaseMapper.deletePesticidePurchaseByIds(pesticidePurchaseIds);

    }

    /**
     * 删除农药购买信息信息
     *
     * @param pesticidePurchaseId 农药购买信息ID
     * @return 结果
     */
    @Override
    public int deletePesticidePurchaseById(Long pesticidePurchaseId )
    {
        PesticidePurchase pesticidePurchase = selectPesticidePurchaseById(pesticidePurchaseId);
        PesticideAmount pesticideAmount = new PesticideAmount();
        pesticideAmount.setPesticideTypeId(pesticidePurchase.getPesticideTypeId());
        pesticideAmount.setPesticideNameId(pesticidePurchase.getPesticideNameId());
        pesticideAmount.setOriginCompany(pesticidePurchase.getOriginCompany());
        pesticideAmount.setContent(pesticidePurchase.getContent());
        List<PesticideAmount> amounts = pesticideAmountMapper.selectPesticideAmountList(pesticideAmount);
        PesticideAmount pesticideAmount1 = amounts.get(0);
        pesticideAmount1.setStorageAmount(pesticideAmount1.getStorageAmount() - pesticidePurchase.getPurchaseAmount());
        pesticideAmountMapper.updatePesticideAmount(pesticideAmount1);
        return pesticidePurchaseMapper.deletePesticidePurchaseById(pesticidePurchaseId);
    }

    /**
     * 插入农药购买表更新农药量表
     * @param pesticidePurchase
     * @return
     */
    @Override
    public int insertPurchaseUpdateAmount(PesticidePurchase pesticidePurchase){
        PesticideAmount pesticideAmount = new PesticideAmount();
        pesticideAmount.setPesticideTypeId(pesticidePurchase.getPesticideTypeId());
        pesticideAmount.setPesticideNameId(pesticidePurchase.getPesticideNameId());
        pesticideAmount.setContent(pesticidePurchase.getContent());
        pesticideAmount.setOriginCompany(pesticidePurchase.getOriginCompany());
        List<PesticideAmount> pesticideAmounts = pesticideAmountMapper.selectPesticideAmountList(pesticideAmount);
        pesticideAmount.setStorageAmount(pesticidePurchase.getPurchaseAmount());
        if (!StringUtils.isEmpty(pesticideAmounts)) {
            PesticideAmount amount = pesticideAmounts.get(0);
            amount.setStorageAmount(amount.getStorageAmount() + pesticidePurchase.getPurchaseAmount());
            pesticideAmountMapper.updatePesticideAmount(amount);
        } else {
            pesticideAmountMapper.insertPesticideAmount(pesticideAmount);
        }
        return insertPesticidePurchase(pesticidePurchase);
    }
}
